/**
 * There are arthur code generation
 */
package	com.arthur.shopping.service.impl;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import tk.mybatis.mapper.entity.Example;

import com.arthur.common.service.impl.BaseService;
import com.arthur.shopping.dao.GoodsMapper;
import com.arthur.shopping.domain.Goods;
import com.arthur.shopping.domain.Type;
import com.arthur.shopping.domain.TypeGoods;
import com.arthur.shopping.service.GoodsService;
import com.arthur.shopping.service.TypeGoodsService;
import com.arthur.shopping.service.TypeService;

@Service("goodsService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class GoodsServiceImpl extends BaseService<Goods> implements GoodsService {

	@Autowired
	private GoodsMapper goodsMapper;
	
	@Autowired
	private TypeGoodsService typeGoodsService;
	
	@Autowired
	private TypeService typeService;
	
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void deletes(String ids) {
		List<String> list = Arrays.asList(ids.split(","));
		typeGoodsService.batchDelete(list, "goodsid", TypeGoods.class);
		this.batchDelete(list, "id", Goods.class);
	}


	@Override
	public void save(Goods goods, String parentId) {
		StringBuilder sb = new StringBuilder("");
		save(goods);
		if(StringUtils.isNotBlank(parentId)){
			sb.append("-");
			String[] typeidArr = parentId.split(",");
			for (String idStr : typeidArr) {
				try{
					long typeid = Long.parseLong(idStr);
					
					Type t = typeService.findById(typeid);
					sb.append(t.getTypeName()).append("-");
					
					TypeGoods typeGoods = new TypeGoods();
					typeGoods.setGoodsid(goods.getId());
					typeGoods.setTypeid(typeid);
					typeGoodsService.save(typeGoods);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
		goods.setTypeIds(sb.toString());
		updateNotNull(goods);
	}


	@Override
	public void updateNotNull(Goods goods, String parentId) {
		StringBuilder sb = new StringBuilder("");
		
		Example example = new Example(TypeGoods.class);
		example.createCriteria().andCondition("goodsId =", goods.getId());
		List<TypeGoods> typeGoodsList = typeGoodsService.selectByExample(example);
		typeGoodsService.deletes(typeGoodsList);
		
		if(StringUtils.isNotBlank(parentId)){
			sb.append("-");
			String[] typeidArr = parentId.split(",");
			for (String idStr : typeidArr) {
				try{
					long typeid = Long.parseLong(idStr);
					
					Type t = typeService.findById(typeid);
					sb.append(t.getTypeName()).append("-");
					
					TypeGoods typeGoods = new TypeGoods();
					typeGoods.setGoodsid(goods.getId());
					typeGoods.setTypeid(typeid);
					typeGoodsService.save(typeGoods);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
		
		goods.setTypeIds(sb.toString());
		updateNotNull(goods);
	}

}
